/**
 * phi
 */

(function(){
    
    var TEXTURE_LIST = {}
    setTimeout(function(){
        var game = enchant.Game.instance;
        if (!game) {
            setTimeout(arguments.callee, 1);
            return ;
        }
        
        /**
         * 動的にパーティクル用の画像を作る
         */
        var width = 128;
        var height= 128;
        var radius= 64;
                
        // 四角形
        var rect = new Surface(width, height);
        var rect_context = rect.context;
        rect_context.fillStyle = "#fff";
        rect_context.fillRect(0, 0, width, height);
        console.dir(rect);
        rect._element.src="hoge";   // これがないとおかしくなる
        TEXTURE_LIST["rect"] = new enchant.gl.Texture(rect);
        
        // サークル
        var circle = new Surface(width, height);
        var circle_context = circle.context;
        circle_context.fillStyle = "#fff";
        circle_context.beginPath();
        circle_context.arc(width/2, height/2, radius, 0, Math.PI*2, false);
        circle_context.fill();
        TEXTURE_LIST["circle"] = new enchant.gl.Texture(circle);
        
        
        // 画像からテクスチャを作成
        TEXTURE_LIST["particle"] = new enchant.gl.Texture("texture/Particle.png");
    }, 1);
    
    enchant.gl.FireEffect = Class.create(enchant.gl.Sprite3D, {
        initialize: function() {
            Sprite3D.call(this);
            
            var self = this;
            this.particles = [];
            this.step = 100;
            
            for (var i=0; i<16; ++i) {
                setTimeout(function(){
                    var particle = new Particle(self.createParam());
                    particle.onendanimation = function() {
                        this.set( self.createParam() );
                    };
                    self.addChild(particle);
                }, i*this.step);
            }
        },
        
        createParam: function() {
            var rad = Math.random()*Math.PI*2;
            var x = Math.cos(rad)*Math.random()*0.5;
            var y = Math.sin(rad)*Math.random()*0.5;
            var param = {
                x: x,
                y: y,
                z: 0,
                vx: Math.random()*0.008-0.004,
                vy: 0.01,
                vz: 0,
                ax: 0,
                ay: 0,
                az: 0,
                life: 120+Math.random()*60,
            };
            
            return param;
        },
        
        /**
         * こいつだけビルボード
         */
        onrender: function() {
            var game = enchant.Game.instance;
            if (game.currentScene3D._camera) {
                this.rotation = game.currentScene3D.cameraMatInverse;
            }
        }
    });
    
    enchant.gl.Particle = Class.create(enchant.gl.primitive.PlaneXY, {
        initialize: function(param) {
            PlaneXY.call(this);
            
            this.scale(1.25, 1.25, 1.25);
            mat4.rotateZ(this.rotation, Math.random()*360*Math.PI/180);
            
            this.set(param);
            this.time = 0;
            
            this.mesh.setBaseColor([1.0, 0.25, 0.01, 0.0]);
            this.mesh.texture = TEXTURE_LIST["circle"];
            this.mesh.texture.ambient = [1.0, 1.0, 1.0, 1.0];
            this.mesh.texture.diffuse = [0.0, 0.0, 0.0, 1.0];
            this.mesh.texture.specular = [0.0, 0.0, 0.0, 1.0];
            this.mesh.texture.shininess = 255;
        },
        
        set: function(param)
        {
            for (var key in param) {
                this[key] = param[key];
            }
        },
        
        onenterframe: function() {
            if (this.time >= this.life) {
                this.time = 0;
                var evt = new enchant.Event("endanimation");
                this.dispatchEvent(evt);
            }
            
            this.x += this.vx;
            this.y += this.vy;
            this.z += this.vz;
            
            this.vx += this.ax;
            this.vy += this.ay;
            this.vz += this.az;
            
            var a = 1.0-(this.time/this.life);
            var a = Math.sin(Math.PI*this.time/this.life);
            this.mesh.setBaseColor([1.0, 0.25, 0.01, a]);
            
            ++this.time;
        },
        
        onendanimation: function() {
            
        },
        
        ondrawbefore: function() {
            gl.blendFunc(gl.SRC_ALPHA, gl.ONE);
            gl.enable(gl.BLEND);
            gl.disable(gl.DEPTH_TEST)
        },
        
        ondrawafter: function() {
            gl.blendFunc(gl.SRC_ALPHA, gl.ONE);
            gl.disable(gl.BLEND);
            gl.enable(gl.DEPTH_TEST)
        },
        
    });
    
})();